package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.oa.modules.base.entity.AppVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("AppVersionDao")
public class AppVersionDao implements CrudDao<AppVersion> {
    private final static Logger logger = LoggerFactory.getLogger(AppVersionDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        for (String key : queryHash.keySet()) {
            String value = queryHash.get(key);
            switch (key) {
                case "searchCode":
                    conditionHash.put("s.version like ?{paramIndex}", "%" + value + "%");
                    continue;
                case "deviceType":
                    conditionHash.put("s.deviceType = ?{paramIndex}", value);
                    continue;
                case "surroundings":
                    conditionHash.put("s.surroundings = ?{paramIndex}", value);
                    continue;
                case "versionCode":
                    conditionHash.put("s.versionCode > ?{paramIndex}", value);
                    continue;
            }
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from AppVersion s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<AppVersion> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from AppVersion s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<AppVersion> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, AppVersion.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public AppVersion findModel(Long id) {
        return entityManager.find(AppVersion.class, id);
    }

    @Override
    public Integer add(AppVersion model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("AppVersionDaoImpl添加AppVersion成功！");
        return 1;
    }

    @Override
    public Integer update(AppVersion model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        AppVersion appVersion = entityManager.find(AppVersion.class, id);
        entityManager.remove(appVersion);
        return 1;
    }

    @Override
    public Integer deleteLogic(AppVersion model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
